#region imported Namespaces
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

using SplitConstants = PHSRAG.Insight21.Personnel.Constants.DistributionSplitsConstants;
#endregion

namespace PHSRAG.Insight21.Personnel
{
	#region DistributionSplits class
	/// <summary>
	/// The DistributionSplits class supports the loading of distribution split
	/// information in relation to the distribution period or salary distribution.
	/// This class acts as a container for DistributionSplit objects
	/// It also provides indexing support to access the contained distribution split objects
	/// </summary>
	public class DistributionSplits : IEnumerable
	{
		#region Instance Variables
		private ArrayList splits;
		#endregion

		#region Public properties
		/// <summary>
		/// Get the count of DistributionSplit objects contained
		/// </summary>
		public int Count
		{
			get{return splits.Count;}
		}
		#endregion

		#region Indexers
		/// <summary>
		/// Retrieve a DistributionSplit from the DistributionSplits instance, given 
		/// its ordinal position within the collection. If the ordinal position is 
		/// out of region, a null cast exception is thrown 
		public DistributionSplit this[int index]
		{
			get{return (DistributionSplit)splits[index];}
		}
		#endregion

		#region Constructor
		/// <summary>
		/// Default constructor
		/// </summary>
		public DistributionSplits()
		{
		}
		#endregion
		
		#region IEnumerable Members
		/// <summary>
		/// Get the enumerator associated with the instance
		/// </summary>
		/// <returns>Object of type IEnumerator</returns>
		public IEnumerator GetEnumerator()
		{
			return splits.GetEnumerator();
		}
		#endregion

		#region Public instance methods

		/// <summary>
		/// Get the Distribution Split object from the collection
		/// that has the specified slit id.  If the distribution is not
		/// found, an error is thrown.
		/// </summary>
		/// <param name="splitID">Primary Key Id of the Distribution Split Object to be returned.</param>
		/// <returns>A DistributionSplit object.</returns>
		public DistributionSplit GetDistributionSplit(int splitID)
		{
			foreach(DistributionSplit split in splits)
			{
				if (split.SplitID == splitID)
					return split;
			}
			
			throw new Exception(String.Format("The distribution does not contain a split with an ID = ({0}", splitID.ToString()));
		}

		/// <summary>
		/// Load the distribution splits
		/// </summary>
		/// <param name="dataTable">Datatable containing split data</param>
		public void Load(DataTable dataTable)
		{
			splits = new ArrayList();
			foreach(DataRow splitRow in dataTable.Rows)
			{
				splits.Add(new DistributionSplit(splitRow));
			}
		}
		#endregion
	}

	#endregion
}